<?php

namespace Rblog\Module\Admin\Controller;

class Reptile extends Common {
    
    /**
     * 抓取动作
     */
    public function reptile() {
        
        set_time_limit(9999);

        $cookie = tempnam(sys_get_temp_dir(), 'cookie'); //cookie临时文件
        $ch = curl_init( 'https://www.oschina.net/home/login?goto_page=http%3A%2F%2Fwww.oschina.net%2F' ); //初始化
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回获取的输出文本流
        curl_setopt($ch, CURLOPT_POSTFIELDS, array(
            'email' => '752536996@qq.com',
            'pwd' => '62520d43cc3a64b325a52aa0e6b6f8e7f86cfe6e',
            'verifyCode' => '',
            'save_login' => 1
        )); //发送POST数据
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); //保存获得的cookie
        curl_exec($ch); //执行curl
        curl_close($ch); //关闭curl


        

        /*-----使用COOKIE-----*/
        $ch = curl_init( 'http://www.oschina.net/blog' ); //初始化
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回获取的输出文本流
        curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie);
        $content = curl_exec($ch); //执行curl
        curl_close($ch); //关闭curl
        
        require_once REXZY_ROOT . '/Tool/phpQuery.php';
        \phpQuery::newDocument( $content );
        echo 123;
        $titles = array();
        $url = array();
        pq( '.BlogList li .b' )->each( function( $elem ) use( &$titles, &$url ) {
            $titles[] = pq( $elem )->find( 'a' )->html();
            $url[] = pq( $elem )->find( 'a')->attr( 'href' );
        } );
        print_r($titles);exit;
        foreach( $url as $key => $val ){
            $ch = curl_init( "$val" ); //初始化
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回获取的输出文本流
            curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie);
            $content = curl_exec($ch); //执行curl
            curl_close($ch); //关闭curl
            preg_match( '/location\.href=\"(.+?)\"/', $content, $matches);
            
            $url[$key] = $val . $matches[1];
        }
        
        $arts = array();
        foreach( $url as $key => $val ){
            $ch = curl_init( "$val" ); //初始化
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回获取的输出文本流
            curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie);
            $content = curl_exec($ch); //执行curl
            curl_close($ch); //关闭curl
            \phpQuery::newDocument( $content );
            $arts[$key]['content'] = pq('.BlogContent')->html();
            $arts[$key]['url'] = "<p>转载自：$val</p>";
        }
        
        $artData = new \Rblog\Module\Admin\Model\Arts();
        foreach ( $titles as $key => $val ) {
            $exist = $artData->select( 'artid', array( 'title' => $val ) );
            if ( count( $exist ) == 0 ) {
                $newArt = array(
                    'title' => "转：" . $val,
                    'sort' => 2,
                    'author' => 'rexzy',
                    'date' => time(),
                    'content' => $arts[$key]['content'] . $arts[$key]['url'],
                    'outline' => '测试爬虫',
                );
                $artData->addArt( $newArt );
            } else {
                continue;
            }
        }
    }
}